package com.swang.pms.web.controller.common;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.swang.pms.common.util.EnumToDataUtil;
import com.swang.pms.common.util.LoggerUtil;
import com.swang.pms.model.annotation.PltLog;
import com.swang.pms.model.common.Pager;
import com.swang.pms.model.dto.common.SysDictionaryDto;
import com.swang.pms.model.dto.common.SysParamDto;
import com.swang.pms.model.entity.CurrentKey;
import com.swang.pms.model.enums.*;
import com.swang.pms.model.vo.ResultMessage;
import com.swang.pms.service.common.SysDictionaryService;
import com.swang.pms.service.common.SysParamService;
import com.swang.pms.web.controller.BaseController;

/**
 * @ClassName: SysParamController
 * @Description: 系统参数服务控制层
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
@Controller
@RequestMapping("/param")
public class SysParamController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(SysParamController.class);

    @Resource
    SysParamService sysParamService;

    @Resource
    SysDictionaryService sysDictionaryService;

    @RequestMapping(value = "/page")
    public ModelAndView page(ModelMap model) {
        SysDictionaryDto sysDictionaryDto = new SysDictionaryDto();
        sysDictionaryDto.setStatus(Status.PUBLISH.getCode());
        sysDictionaryDto.setGroupCode(DictionaryGroup.BIZ_MODULE.getCode());
        String bizModuleComboData = getComboDataFromDict(sysDictionaryDto, "");
        model.put("bizModuleComboData", bizModuleComboData);
        String sysTypeComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(SysType.class);
        model.put("sysTypeComboDatas", sysTypeComboData);
        String dataTypeComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(DataType.class);
        model.put("dataTypeComboData", dataTypeComboData);
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("param/sysParamMgr", model);
    }

    @PltLog(log = "查询参数管理列表信息")
    @RequestMapping(value = "/findSysParamGrid")
    public @ResponseBody Pager findSysParamGrid(Pager pager, SysParamDto dto) {
        return this.sysParamService.pageFindSysParamGrid(pager, dto);
    }

    @RequestMapping(value = "/openEditSysParamPage")
    public ModelAndView openEditSysParamPage(ModelMap model, String type, String dataType) {
        model.put("type", type);
        model.put("dataType", dataType);
        return new ModelAndView("param/win/editSysParam", model);
    }

    @PltLog(log = "启用/停用参数")
    @RequestMapping(value = "/doUndoSysParam")
    public @ResponseBody ResultMessage doUndoSysParam(@RequestBody SysParamDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "操作成功");
        try {
            CurrentKey key = new CurrentKey();
            key.setId(dto.getId());
            key.setCode(dto.getParamCode());
            dto.setGmtModify(new Date());
            this.sysParamService.updateByPrimaryKey(dto);
        } catch (Exception e) {
            result.change(false, "操作失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysParam-doUndoSysParam error:{0} param:{1}", e.getMessage(), dto);
        }
        return result;
    }

    @PltLog(log = "编辑参数")
    @RequestMapping(value = "/editSysParam")
    public @ResponseBody ResultMessage editSysParam(@RequestBody SysParamDto sysParamDto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "更新成功");
        CurrentKey currentKey = new CurrentKey();
        currentKey.setId(sysParamDto.getId());
        try {
            sysParamDto.setGmtModify(new Date());
            this.sysParamService.updateByPrimaryKey(sysParamDto);
        } catch (Exception e) {
            result.change(false, "更新失败" + e.getMessage());
            LoggerUtil.error(LOGGER, "dict-editSysDict error: {0}", e.getMessage());
        }
        return result;
    }

    public String getComboDataFromDict(SysDictionaryDto sysDictionaryDto, String selectValue) {
        return getComboDataFromDictList(sysDictionaryService.getSysDictionaryList(sysDictionaryDto), selectValue);
    }

}
